package com.sanpu.kxe.business.main.presenter;

import android.text.TextUtils;

import com.sanpu.kxe.business.main.bean.MyProfitsOutput;
import com.sanpu.kxe.business.main.bean.ProfitsOutput;
import com.sanpu.kxe.business.main.contract.MyProfitsContract;
import com.sanpu.kxe.business.main.model.MainModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.jump.eventbus.RefreshPersonEvent;
import com.sanpu.kxe.jump.eventbus.RefreshProfitsEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：我的盟友列表（收益）
 * Created by 薛勇军 on 2018/8/8.
 */

public class MyProfitsPresenter implements MyProfitsContract.Presenter {

    private MyProfitsContract.View mView;

    private CompositeSubscription mSubscription;

    private String mMerchantCode, mMerchantName, mAccountName;

    private int mCurLvl, mPersonCount;

    public MyProfitsPresenter(MyProfitsContract.View mView, int mCurLvl) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
        this.mCurLvl = mCurLvl;
    }

    @Override
    public void initParamsData(String mMerchantCode, String mAccountName) {
        this.mMerchantCode = mMerchantCode;
        this.mAccountName = mAccountName;
    }

    @Override
    public void getMyProfitsTeam(final int nowPage) {
        Subscription subscription = MainModel.getInstance()
                .myTeamList(nowPage, mMerchantCode, mCurLvl, mAccountName)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MyProfitsOutput>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showLoadMore(CommonConfig.LOADMOREFAIL);
                    }

                    @Override
                    public void onNext(MyProfitsOutput myProfitsOutput) {
                        if (myProfitsOutput != null) {
                            mPersonCount = myProfitsOutput.getCountTotal();
                            EventBus.getDefault().post(new RefreshPersonEvent(mPersonCount));
                            List<ProfitsOutput> profitsOutputs = myProfitsOutput.getTeamInfo();
                            if (profitsOutputs != null) {
                                mView.showProfitsTeamView(profitsOutputs, nowPage == 0);
                                if (profitsOutputs.size() < CommonConfig.PAGENUMER) {
                                    mView.showLoadMore(CommonConfig.LOADMOREEND);
                                } else {
                                    mView.showLoadMore(CommonConfig.LOADMORECOMPLETE);
                                }
                            } else {
                                mView.showLoadMore(CommonConfig.LOADMOREEND);
                            }
                        } else {
                            mView.showLoadMore(CommonConfig.LOADMOREEND);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onItemClick(ProfitsOutput output) {
        if (output != null) {
            mMerchantName = output.getuName();
            if (!TextUtils.isEmpty(output.getCrpNm()))
                mMerchantName = output.getCrpNm();
            if (output.getRefCount() != 0) {
                EventBus.getDefault().post(new RefreshProfitsEvent(mCurLvl, output.getUsrNo(),
                        mMerchantName, output.getuAccountName()));
            }
        }
    }

    @Override
    public String getMerchantName() {
        return mMerchantName;
    }

    @Override
    public int getPersonCount() {
        return mPersonCount;
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
